package com.campus.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campus.app.service.FunctionServer;
import com.campus.commons.entity.Course;
import com.campus.app.mapper.CourseMapper;
import com.campus.app.service.CoursesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.commons.utils.Error;
import com.campus.commons.utils.MyPage;
import com.campus.commons.utils.Result;
import com.campus.commons.utils.Tool;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程表 服务实现类
 * </p>
 *
 * @author Rin
 * @since 2020-12-24
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CoursesService {
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private FunctionServer functionServer;


    @Override
    public Result<List<Course>> getCourseByTeacherId(Integer teacherId) {
        // teacherId=3 order by date,isForenoon
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacherId", teacherId).orderByAsc("date", "isForenoon");
        return Result.result(courseMapper.selectList(queryWrapper));
    }

    @Override
    public Result<Map<Integer,List<Course>>> getCoursesByClassId(Integer... classId) {
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("classId", classId);
        List<Course> courses = courseMapper.selectList(queryWrapper);
        return Result.success(courses.stream().collect(Collectors.groupingBy(Course::getClassid)));
    }

    @Override
    public Result<List<Course>> getNowCourse() {
        String now = Tool.getDateToString(null);
        LambdaQueryWrapper<Course> lambda = new QueryWrapper<Course>().lambda();
        lambda.eq(Course::getDate, now).eq(Course::getIsforenoon, isForenoon() ? 1 : 2);
        return Result.result(courseMapper.selectList(lambda));
    }

    private boolean isForenoon() {
        int i=    Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
        if (i < 12) {
            return true;
        }
        return false;
    }
    @Override
    public Result<List<Course>> getCourseByClassId(Integer classId) {
        //week(date)=week(now())
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("week(date)", "week(now()").eq("classId", classId);
        return Result.result(courseMapper.selectList(queryWrapper));
    }

    @Override
    public Result<List<Course>> getCourse(MyPage myPage) {
        return Result.result(courseMapper.selectPage(new Page<Course>(myPage.getPage(), myPage.getLimit()), null));
    }

    @Override
    public Result<List<Course>> getCourseBy(MyPage myPage, String start, String end, Integer classId) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        if (StringUtils.isEmpty(end) && !StringUtils.isEmpty(start)) {
            wrapper.ge("date", start);
        }
        if (!StringUtils.isEmpty(end) &&StringUtils.isEmpty(start)) {
            wrapper.ge("date", end);
        }
        if (!StringUtils.isEmpty(end) && StringUtils.isEmpty(start)) {
            wrapper.ge("date", start);
            wrapper.le("date", end);
        }
        if (StringUtils.isEmpty(end) && StringUtils.isEmpty(start) && classId == null) {
            return getCourse(myPage);
        }
        if (classId!=null&&classId>=1){
            wrapper.eq("classId", classId);
        }
        return Result.result(courseMapper.selectPage(new Page<Course>(myPage.getLimit(), myPage.getPage()), wrapper));
    }


    @Override
    public Result addCourse(@NotNull  Course course) {
        int insert = courseMapper.insert(course);
        return Result.result(insert);
    }

    @Override
    public Result addCourses(InputStream upFile, String fileName) {
        try {
            List<Course> list = functionServer.upLoadCourse(upFile, fileName);
            Integer total = list.size();
            Integer succeed = courseMapper.InsertCourses(list);
           return Result.result(total, succeed);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error(Error.System,null);
        }
    }

    @Override
    public Result addCourses(@NotNull List<Course> courses) {
        int i = courseMapper.InsertCourses(courses);
        return Result.result(i);
    }

    @Override
    public Result upCourse(@NotNull Course course) {
        int i = courseMapper.updateById(course);
        return Result.result(i);
    }

    @Override
    public Result delCourse(@Min(1) List<Integer> id) {

        return Result.result(courseMapper.deleteBatchIds(id));
    }





}
